package com.movie.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.movie.model.auto.FilmCategory;
import com.movie.model.auto.Role;
import com.movie.service.IFilmCategoryService;
import com.movie.service.IRoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 电影类型表 前端控制器
 * </p>
 *
 * @author 达瓦里氏
 * @since 2021-12-16
 */
@RestController
@RequestMapping("/category")
@CrossOrigin
public class FilmCategoryController {

    @Resource
    private IFilmCategoryService iFilmCategoryService;

    /*查询类别*/
    @GetMapping("/getFilmCategory")
    public List<FilmCategory> getFilmCategory() {
        return iFilmCategoryService.list();
    }

    /*通过id查询类别*/
    @GetMapping("/getFilmCategoryById/{id}")
    public FilmCategory getFilmCategoryById(@PathVariable("id") int id) {
        return iFilmCategoryService.getById(id);
    }

    /*修改类别*/
    @PostMapping("/updateFilmCategory")
    public void updateFilmCategory(@RequestBody FilmCategory filmCategory) {
        iFilmCategoryService.updateById(filmCategory);
    }

    /*添加类别*/
    @PostMapping("/setFilmCategory")
    public void setFilmCategory(@RequestBody FilmCategory filmCategory) {
        iFilmCategoryService.save(filmCategory);
    }

    /*删除类别*/
    @PostMapping("/deletedById/{id}")
    public boolean deleted(@PathVariable("id") int id) {
        return iFilmCategoryService.removeById(id);
    }


    /*批量删除类别*/
    @PostMapping("/deletedAllById")
    public boolean deletedAllById(@RequestBody int[] delarr){
        for (int i : delarr) {
            System.out.println(i);
            iFilmCategoryService.removeById(i);
        }
        return true;
    }

    /*模糊查询电影分类数据*/
    @PostMapping("/selectFilmCategory/{input}")
    public List<Map<String, Object>> selectFilmCategory(@PathVariable("input") String value){
        System.out.println(value);
        QueryWrapper<FilmCategory> queryWrapper = new QueryWrapper<>();
        /*核心模糊算法*/
        queryWrapper.like("name",value).or().like("code",value);
        List<Map<String, Object>> maps = iFilmCategoryService.listMaps(queryWrapper);
        return maps;
    }

}
